package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.DistrictService;
import com.asm.vo.sys.DistrictVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author tanzujie
 */
@RestController
public class DistrictController extends BaseController {
    @Resource
    private DistrictService districtService;

    /**
     * 根据所选省获取所有的城市
     *
     * @param province 省份
     * @return 城市
     */
    @ApiOperation("接口描述:根据所选的省获取所有的城市")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "province", value = "省份")
    })
    @GetMapping("/cities/province/{province}")
    public ResponseEntity<Result> getAllCityByProvince(@PathVariable("province") String province) {
        return new ResponseEntity(new Result().ok("success", districtService.getAllCityByProvince(province)), HttpStatus.OK);
    }

    /**
     * 根据城市名字分页查询省市区
     *
     * @param city 城市名字
     * @return 省市区
     */
    @ApiOperation("接口描述:根据城市名字分页查询省市区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "city", value = "城市名字")
    })
    @GetMapping("/districts/city/{city}")
    public ResponseEntity<PageResult<DistrictVo>> getfindDistrictByCity(@PathVariable("city") String city) {
        return new ResponseEntity(new Result().ok("success", districtService.findDistrictByCity(city, 0, 50)), HttpStatus.OK);
    }

    /**
     * 分页查询所有省市区
     *
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 省市区
     */
    @ApiOperation("接口描述:分页查询所有省市区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/districts")
    public ResponseEntity<PageResult<DistrictVo>> getAllDistricts(Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", districtService.findAllDistricts(pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据Id获取省市区
     *
     * @param id 编号
     * @return 省市区
     */
    @ApiOperation("接口描述:根据Id获取省市区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号")
    })
    @GetMapping("/district/{id}")
    public ResponseEntity<Result<DistrictVo>> getDistrictById(@PathVariable("id") Long id) {
        return new ResponseEntity(new Result().ok("success", districtService.findDistrictById(id)), HttpStatus.OK);
    }

    /**
     * 根据省名字类型分页查询省市区
     *
     * @param province   省份
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 省市区
     */
    @ApiOperation("接口描述:根据省名字类型分页查询省市区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "province", value = "省份"),
            @ApiImplicitParam(name = "pageNumber", value = "当前页码"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/districts/province/{province}")
    public ResponseEntity<PageResult<DistrictVo>> getDistrictsByType(@PathVariable("province") String province, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", districtService.findDistrictByProvince(province, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 获取数据库中所有地区的省份
     *
     * @return 省份列表
     */
    @ApiOperation("接口描述:获取数据库中所有地区的省份")
    @GetMapping("/district/getAllProvinces")
    public ResponseEntity<Result> getAllProvinces() {
        return new ResponseEntity<>(new Result().ok("success", districtService.getAllProvinces()), HttpStatus.OK);
    }

    /**
     * 获取数据库中所有地区的城市
     *
     * @param province 省份
     * @return 省份列表
     */
    @ApiOperation("接口描述:获取数据库中所有地区的城市")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "province", value = "省份")
    })
    @GetMapping("/district/getAllCitiesByProvinces/{province}")
    public ResponseEntity<Result> getAllCities(@PathVariable("province") String province) {
        return new ResponseEntity<>(new Result().ok("success", districtService.getAllCitiesByProvince(province)), HttpStatus.OK);
    }

    /**
     * 获取数据库中所有地区的地区
     *
     * @param city 城市
     * @return 省份列表
     */
    @ApiOperation("接口描述:获取数据库中所有地区的地区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "city", value = "城市")
    })
    @GetMapping("/district/getAllDistrictsByCity/{city}")
    public ResponseEntity<Result> getAllDistricts(@PathVariable("city") String city) {
        return new ResponseEntity<>(new Result().ok("success", districtService.getAllDistrictsByCity(city)), HttpStatus.OK);
    }

    /**
     * 增加省市区
     *
     * @param districtVo 省市区对象
     * @return 操作状态
     */
    @ApiOperation("接口描述:增加省市区")
    @PostMapping("/district/add")
    public ResponseEntity<Result> addDistrict(@RequestBody @ApiParam(name = "districtVo", value = "省市区对象") DistrictVo districtVo) {
        districtService.addDistrict(districtVo);
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 更新省市区
     *
     * @param districtVo 省市区对象
     * @return 操作状态
     */
    @ApiOperation("接口描述:更新省市区")
    @PutMapping("/district/update")
    public ResponseEntity<Result> updateDistrict(@RequestBody @ApiParam(name = "districtVo", value = "省市区对象") DistrictVo districtVo) {
        districtService.updateDistrict(districtVo);
        return new ResponseEntity(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据Id删除省市区
     *
     * @param id 编号
     * @return 操作状态
     */
    @ApiOperation("接口描述:根据Id删除省市区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号")
    })
    @DeleteMapping("/district/delete/{id}")
    public ResponseEntity<Result> deleteDistrict(@PathVariable("id") Long id) {
        districtService.deleteDistrict(id);
        return new ResponseEntity(new Result().ok("delete success"), HttpStatus.OK);
    }

}
